<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class forum_controller_usertitles extends omnitrix_controller
{
	public function __construct()
	{
		$this->_init_response();
	}

	protected function _init_response()
	{
		$active_tab = otx_router_controller() . '_' . otx_router_action();

		$navtabs['usertitles_index'] = array($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'));
		$navtabs['usertitles_add'] = array($this->_('navtab_add_title'), $this->_('navtab_add_desc'), otx_router_url('add'));

		if (otx_router_action() == 'edit') {
			$navtabs['usertitles_edit'] = array($this->_('navtab_edit_title'), $this->_('navtab_edit_desc'), otx_request_uri());
		}

		if (!array_key_exists($active_tab, $navtabs)) {
			return;
		}

		foreach ($navtabs as $name => $tab) {
			$is_active = $name == $active_tab;
			otx_html_add_navtab($tab[0], $tab[1], $tab[2], $is_active);
			if ($is_active) {
				otx_html_response_title($tab[0]);
			}
		}

		otx_html_add_breadcrumb($this->_('text_breadcrumb'), otx_router_url('index'));
		otx_html_add_breadcrumb($navtabs[$active_tab][0]);
	}

	public function index_action()
	{
		$list_sql = 'SELECT * FROM mybb_usertitles';
		$list_rows = otx_db_fetch_all($list_sql);

		$list_table = otx_html_create_table($this->_('navtab_index_title'), 'table-list');
		$list_table->set_head_cell($this->_('column_title'));
		$list_table->set_head_cell($this->_('column_posts'), array('class' => 'center'));
		$list_table->set_head_cell($this->_('text_actions'), array('class' => 'center', 'colspan' => 2));

		if ($list_rows) {
			foreach ($list_rows as $row) {
				$list_table->add_body_row();
				$list_table->set_body_cell(otx_html_link($row['title'], array('href' => otx_router_url('edit', null, null, array('utid' => $row['utid']))), true));
				$list_table->set_body_cell($row['posts'], array('class' => 'center', 'width' => '35%'));
				$list_table->set_body_cell(otx_html_link($this->_('text_edit'), array('href' => otx_router_url('edit', null, null, array('utid' => $row['utid'])))), array('class' => 'center', 'width' => 100));
				$list_table->set_body_cell(otx_html_link($this->_('text_delete'), array('href' => otx_router_url('delete', null, null, array('utid' => $row['utid'])), 'onclick' => 'return AdminCP.confirmPost(this, JSTEXTS.confirm_delete)')), array('class' => 'center', 'width' => 100));
			};
		} else {
			$list_table->add_body_row();
			$list_table->set_body_cell($this->_('text_no_results'), array('colspan' => 4));
		}
	}

	public function add_action()
	{
		$this->edit_action();
	}

	public function edit_action()
	{
		$utid = otx_param_int('utid');

		if ($utid) {
			$sql = 'SELECT * FROM mybb_usertitles WHERE utid = ?';
			$row = otx_db_fetch_row($sql, $utid);
			if (false == $row) {
				otx_admin_flash_warning($this->_('error_invalid_id', $utid));
				otx_router_redirect('index');
			}
		}

		if (otx_request_is_post()) {
			$vars['title'] = otx_param_string('title');
			$vars['posts'] = otx_param_int('posts');
			$vars['stars'] = otx_param_int('stars');
			$vars['starimage'] = otx_param_string('starimage');

			if (otx_validate_is_empty($vars['title'])) {
				$errors[] = $this->_('error_title_empty');
			}

			if (empty($errors)) {
				if ($utid) {
					otx_db_update('mybb_usertitles', $vars, array('utid' => $utid));
					$vars['utid'] = $utid;
					otx_admin_flash_message($this->_('text_success_edit', $vars));
					otx_admin_log_action($this->_('text_success_edit', $vars));
				} else {
					otx_db_insert('mybb_usertitles', $vars);
					$vars['utid'] = otx_db_last_inserted_id();
					otx_admin_flash_message($this->_('text_success_add', $vars));
					otx_admin_log_action($this->_('text_success_add', $vars));
				}

				otx_router_redirect('index');
			}
		} else {
			if ($utid) {
				$vars['title'] = $row['title'];
				$vars['posts'] = $row['posts'];
				$vars['stars'] = $row['stars'];
				$vars['starimage'] = $row['starimage'];
			} else {
				$vars['title'] = '';
				$vars['posts'] = '';
				$vars['stars'] = '';
				$vars['starimage'] = '';
			}

			$errors = array();
		}

		$form_wrapper = otx_html_create_wrapper()->set_form_errors($errors);
		$form_table = $form_wrapper->create_table($utid ? $this->_('navtab_edit_title') : $this->_('navtab_add_title'), 'table-form');
		$form_table->add_form_input($this->_('entry_title'), otx_html_form_text('title', $vars['title']), true);
		$form_table->add_form_input($this->_('entry_posts'), otx_html_form_text('posts', $vars['posts']), false);
		$form_table->add_form_input($this->_('entry_stars'), otx_html_form_text('stars', $vars['stars']), false);
		$form_table->add_form_input($this->_('entry_starimage'), otx_html_form_text('starimage', $vars['starimage']), false);
	}

	public function delete_action()
	{
		$utid = otx_param_int('utid');

		$sql = 'SELECT * FROM mybb_usertitles WHERE utid = ?';
		$row = otx_db_fetch_row($sql, $utid);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $utid));
			otx_router_redirect('index');
		}

		otx_db_delete('mybb_usertitles', array('utid' => $utid));
		otx_admin_flash_message($this->_('text_success_delete', $row));
		otx_admin_log_action($this->_('text_success_delete', $row));
		otx_router_redirect('index');
	}
}